﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 易容酒店管理系统
{
    public partial class ADD : Form
    {
        public ADD()
        {
            InitializeComponent();
        }
        //关闭
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();
           
        }

        private void ADD_Load(object sender, EventArgs e)
        {
            LookUp();
        }
        public void LookUp()
        {
            listView1.Items.Clear();
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            SqlConnection con = new SqlConnection(Link);
            string Sql = "select * from StaffInfo;";
            SqlDataAdapter Add = new SqlDataAdapter(Sql, con);
            DataSet set = new DataSet();
            Add.Fill(set, "StaffInfo");
            DataTable table = set.Tables["StaffInfo"];
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow drow = table.Rows[i];
                ListViewItem lvi = new ListViewItem(drow["StaffID"].ToString());
                lvi.SubItems.Add(drow["StaffID"].ToString());
                lvi.SubItems.Add(drow["Emplogee_position"].ToString());
                lvi.SubItems.Add(drow["StaffName"].ToString());
                lvi.SubItems.Add(drow["PassWord"].ToString());
                listView1.Items.Add(lvi);
            }
        }

        private void butEntry_Click(object sender, EventArgs e)
        {
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                string Sql = "insert into StaffInfo values('" + txtID.Text + "','" + txtName.Text + "','" + txtPass.Text + "','" + txtEmplogee_position.Text + "');";
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    int num = cmd.ExecuteNonQuery();
                    if (num > 0)
                    {
                        MessageBox.Show("添加成功！");
                    }
                }
            }
            LookUp();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int Index = 0;
            if (this.listView1.SelectedItems.Count > 0)//判断listview有被选中项
            {
                Index = this.listView1.SelectedItems[0].Index;//取当前选中项的index,SelectedItems[0]这必须为0
                String aa = listView1.Items[Index].SubItems[0].Text;//用我们刚取到的index取被选中的某一列的值从0开始
            
                string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
                using (SqlConnection con = new SqlConnection(Link))
                {
                    string Sql = "Delete from StaffInfo where StaffID='" + aa+"';";
                    using (SqlCommand cmd = new SqlCommand(Sql, con))
                    {
                        con.Open();
                        int num = cmd.ExecuteNonQuery();
                        if (num > 0)
                        {
                            MessageBox.Show("删除成功！");
                        }
                    }
                }
            }
            LookUp();
        }
    }
}
